#include "air780e.h"
#include "FreeRTOS.h"
#include "task.h"
#include <string.h>
#include "usart.h"
#include "log.h"

const static char *TAG = "air780e";

/**
 * @brief air780e_init
 * @note  -- air780e初始化配置
 */
void air780e_init(void)
{
    char data[40];
    strcpy(data, "ATI\r\n");
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));

    strcpy(data, "AT+CPIN?\r\n");
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));

    strcpy(data, "AT+CSQ\r\n");
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));

    strcpy(data, "AT+CGATT?\r\n");
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));

    strcpy(data, "AT+CMGF=1\r\n");
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));

    strcpy(data, "AT+CSMP?\r\n");
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));

    strcpy(data, "AT+CSCS?\r\n");
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));

    /*
       trcpy(data, "AT+CSCLK=0\r\n"); // 将Air780e设置为禁用睡眠模式
       HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
       memset(data, 0, sizeof(data));
       vTaskDelay(pdMS_TO_TICKS(100));
    */
}

/**
 * @brief send_command_end_sms
 * @note  -- 短信发送结算指令
 */
static void send_command_end_sms(void)
{
    uint8_t AT_end_cmd = 0x1a; // 要发送的十六进制数
    uint8_t buffer[1];         // 存储要发送的数值
    buffer[0] = AT_end_cmd;    // 存储要发送的数值
    HAL_UART_Transmit(&huart2, buffer, 1, HAL_MAX_DELAY);
}

/**
 * @brief send_dht_sms_app
 * @note  -- air780e短信发送温湿度
 * @param dht_data
 * @param air780e
 */
void send_dht_sms_app(dht_t *dht_data, air780e_t *air780e)
{
    char data[40];

    if (air780e->send_flag)
    {
        /* 发送短信 */
        sprintf(data, "AT+CMGS=\"%s\"\r\n", Mobile_Phone); // 不要有空格
        HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
        memset(data, 0, sizeof(data));
        vTaskDelay(pdMS_TO_TICKS(100));
        /* 发送内容 */
        sprintf(data, "Temp:%.1f C\r\nHumi:%.1f %%", dht_data->temp, dht_data->humi);
        HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
        vTaskDelay(pdMS_TO_TICKS(100));
        /* 发送结束命令 */
        send_command_end_sms();
    }
    else
    {
        /* 无动作 */
    }
}

/**
 * @brief send_warning_sms_app
 * @note  -- 短信发送温度过高警告
 */
void send_warning_sms_app(void)
{
    char data[66];
    /* 发送短信 */
    sprintf(data, "AT+CMGS=\"%s\"\r\n", Mobile_Phone); // 不要有空格
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));
    /* 发送内容 */
    sprintf(data, "The temperature is too high and the device has been turned off!!"); // 发送：温度过高，设备已经关闭
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    vTaskDelay(pdMS_TO_TICKS(100));
    /* 发送结束命令 */
    send_command_end_sms();
}

/**
 * @brief send_abnormal_sms_app
 * @note  -- 短信发送温度异常不可操作
 */
void send_abnormal_sms_app(void)
{
    char data[48];
    /* 发送短信 */
    sprintf(data, "AT+CMGS=\"%s\"\r\n", Mobile_Phone); // 不要有空格
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));
    /* 发送内容 */
    sprintf(data, "Temperature abnormalities are not operable!!"); // 发送：温度异常不可操作
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    vTaskDelay(pdMS_TO_TICKS(100));
    /* 发送结束命令 */
    send_command_end_sms();
}

/**
 * @brief send_ctrl_option_sms_app
 * @note  -- 短信发送控制操作完成
 */
void send_ctrl_option_sms_app(bool option)
{
    char data[28];
    /* 发送短信 */
    sprintf(data, "AT+CMGS=\"%s\"\r\n", Mobile_Phone); // 不要有空格
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    memset(data, 0, sizeof(data));
    vTaskDelay(pdMS_TO_TICKS(100));
    /* 发送内容 */
    switch (option)
    {
    case true:
        sprintf(data, "The device is turned on!!"); // 发送：设备已经打开
        break;
    case false:
        sprintf(data, "The device is turned off!!"); // 发送：设备已经打开
        break;
    default:
        break;
    }
    HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), 1000);
    vTaskDelay(pdMS_TO_TICKS(100));
    /* 发送结束命令 */
    send_command_end_sms();
}

/**
 * @brief usart2_proc
 * @note  -- 短信接收串口处理
 * @param str 传入字符串(符合条件并使能指定参数并清空str指向的字符串)
 * @param index
 * @param air780e
 * @param device
 */
void usart2_proc(char *str, uint8_t *index, air780e_t *air780e, ctrl_t *device)
{
    /* 检测控制命令 */
    if ((strstr(str, "check") != NULL) && (strstr(str, "MESSAGE") != NULL))
    {
        MY_LOGI(TAG, "%s\r\nstart send dht11 sms!\r\n\r\n", str);

        air780e->send_flag = 1; /* air780e发送标志位置1 */

        memset(str, 0, sizeof(str));
        *index = 0;
    }
    else if ((strstr(str, "open") != NULL) && (strstr(str, "MESSAGE") != NULL))
    {
        MY_LOGI(TAG, "%s\r\nstart open the device!\r\n\r\n", str);

        device->device_ctrl = 1; /* 设备控制开启标志位置1 */

        memset(str, 0, sizeof(str));
        *index = 0;
    }
    else if ((strstr(str, "close") != NULL) && (strstr(str, "MESSAGE") != NULL))
    {
        MY_LOGI(TAG, "%s\r\nstart close the device!\r\n\r\n", str);

        device->device_ctrl = 0; /* 设备控制关闭标志位置0 */

        memset(str, 0, sizeof(str));
        *index = 0;
    }
    else if ((strstr(str, "restart") != NULL) && (strstr(str, "MESSAGE") != NULL))
    {
        MY_LOGI(TAG, "%s\r\nstart restart the device!\r\n\r\n", str);

        device->device_restart = 1; /* 设备复位标志位置1 */

        memset(str, 0, sizeof(str));
        *index = 0;
    }
    /* 如果接收到短信但是不是指令的内容则清除 */
    else if ((strstr(str, "close") == NULL) && (strstr(str, "open") == NULL) && (strstr(str, "MESSAGE") != NULL))
    {
        MY_LOGI(TAG, "%s\r\nthe sms is not effective cmd!\r\n\r\n", str);

        memset(str, 0, sizeof(str));
        *index = 0;
    }
    /* 检测Air780E挂机一段时间后发送的串口数据过滤,避免串口卡死 */
    else if ((strstr(str, "+CGEV") != NULL) || (strstr(str, "+NITZ") != NULL))
    {
        MY_LOGI(TAG, "%s\r\nout timing!\r\n\r\n", str);

        memset(str, 0, sizeof(str));
        *index = 0;
    }
}
